package com.mystudy.algorithm.sort;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @program: infoalgorithm
 * @description: 冒泡算法
 * @author: zhouzhilong
 * @create: 2019-07-30 10:02
 **/
public class BubbleSort {
    /** logger */
    private static final Logger LOGGER = LoggerFactory.getLogger(BubbleSort.class);

    @Test
    public void test() {
        int[] arr = new int[]{4, 1, 9, 5, 1, 9, 5, 6, 6, 9, 2, 15, 21, 96, 34};
        int[] sort = sort(arr);
        LOGGER.debug("sort = [{}]",sort);
    }


    private int[] sort(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length - i-1; j++) {
                if (arr[j] > arr[j + 1]) {
                    arr[j + 1] = (arr[j] + arr[j + 1]) - (arr[j] = arr[j + 1]);
                }
            }
        }
        return  arr;
    }

}
